import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    page_control: {
      load: {
        user: false,
        login: false,
        ad: false,
        section: false,
      },
      popup: {
        left: false,
      },
      isRouterAlive:true,
    },
    data: {
      ad: Array,
      userinfo: Object,
      section: Array,
      messagenumber: {
        reply: 0,
        atme: 0,
        system: 0,
        fellow: 0,
        chat: 0,
        total: 0,
      },
    },
  },
  getters: {
    is_pageloaddone: (state) => {
      if (
        state.page_control.load.user &&
        state.page_control.load.ad &&
        state.page_control.load.section
      ) {
        return true;
      } else return false;
    },
    is_login: (state) => {
      return state.page_control.load.login;
    },
    get_msgnum: (state) => {
      if (state.data.messagenumber.total > 99) {
        return "99+";
      } else if (state.data.messagenumber.total > 0) {
        return state.data.messagenumber.total.toString();
      } else {
        return false;
      }
    },
    is_accountvalied: (state) => {
      if (state.page_control.load.login) {
        return (
          state.data.userinfo.phoneverified && state.data.userinfo.emailverified
        );
      } else return true;
    },
  },
  mutations: {
    /**
     * 加载用户登录信息
     * @param {Object} state
     * @param {Boolean} login 是否登录
     * @param {Object} userinfo 用户信息
     */
    user_loaddone(state, data) {
      state.page_control.load.user = true;
      if (data.login) {
        state.data.userinfo = data.userinfo;
        state.page_control.load.login = true;
      }
    },
    /**
     * 加载广告信息
     * @param {Object} state
     * @param {Array} adlist 广告列表
     */
    ad_loaddone(state, adlist) {
      state.page_control.load.ad = true;
      state.data.ad = adlist;
    },
    /**
     *加载板块信息
     * @param {Object} state
     * @param {Array} section 板块列表
     */
    section_loaddone(state, section) {
      state.page_control.load.section = true;
      state.data.section = section;
    },
    /**
     * 加载消息数量
     * @param {Object} state
     * @param {Object} messagenumber 消息数量统计
     */
    messagecount_loaddone(state, messagenumber) {
      state.data.messagenumber = messagenumber;
    },
    open_leftpopup(state) {
      state.page_control.popup.left = true;
    },
    close_leftpopup(state) {
      state.page_control.popup.left = false;
    },
    logout(state) {
      state.page_control.load.login = false;
      state.data.messagenumber = {
        reply: 0,
        atme: 0,
        system: 0,
        fellow: 0,
        chat: 0,
        total: 0,
      };
      state.data.userinfo = {};
    },
  },
  actions: {
  },
  modules: {},
});
